﻿@using KRM.Core;
@using KRM.Core.DataObject
@model IEnumerable<KRMSP_GetPostCodeAlerts_Result>
@{
    var carriers = (List<tblKRMCarrier>)ViewBag.Carriers;
    var delTypes = (List<tblKRMDeliveryType>)ViewBag.DelTypes;
    var carrierReasons = new List<ComboboxStringID>
    {
         new ComboboxStringID(){Id="",Name = ""},
        new ComboboxStringID(){Id="Extra delivery charge applies",Name = "Extra delivery charge applies"},
           new ComboboxStringID(){Id="Cannot deliver to this area",Name = "Cannot deliver to this area"},
    };
}
<script type="text/javascript">
    $(document).ready(function() {
        $('#postcode-item input[checked="checked"]').parent("label").addClass("postcontrol-checked");
        $('#postcode-item input[type="checkbox"]').not(":checked").parent("label").parent().next().children("select").attr('disabled', 'disabled');
        $('#fr-postcodeitem-@Model.First().outcode').on('submit', function() {
            $.ajax({
                url: "/StockControl/SavePostcodeAlertItem",
                data: $('#fr-postcodeitem-@Model.First().outcode').serialize(),
                success: function (data) {
                    
                }
            });
            return false;
        });
    });
    function SubmitPostCode(form) {
        App.blockUI($('body'));
        $(form).submit();
        alert("Success");
        App.unblockUI($('body'));

    }

    $('#postcode-item input[type="checkbox"]').change(function () {
        if ($(this).is(":checked")) {
            $(this).parent("label").addClass("postcontrol-checked");
            $(this).parent("label").parent().next().children("select").removeAttr('disabled');
        } else {
            $(this).parent("label").parent().next().children("select").attr('disabled', 'disabled');
            $(this).parent("label").removeClass("postcontrol-checked");

        }
    });



</script>
<div class="fieldset">
    <div class="legend">
        <span style="font-size: 16px; font-weight: bold">@Model.First().outcode</span>
        <form action="/stockcontrol/SavePostcodeAlertItem" method="post" class="margin0" id="fr-postcodeitem-@Model.First().outcode">
            <input type="hidden" name="charCode" value="@ViewBag.charCode" />
            <input type="hidden" name="outCode" value="@Model.First().outcode" />
            <div class="row-fluid" id="postcode-item">
                <div class="span9 overflow-auto" style="height: 230px; width: 78.35897435897436%">
                    <div class="row-fluid">
                        <div class="span12"><span style="font-size: 14px; font-weight: bold">Carrier</span> </div>
                    </div>

                    @{var d = 0;
                      const string open = "<div class=\"row-fluid\">";
                      const string close = "</div>";
                      var isClose = false;
                    }
                    @foreach (var item in carriers)
                    {
                        d++;
                        if (d == 1)
                        {
                        @Html.Raw(@open)
                            isClose = false;
                        }
                        <div class="span3">
                            <label style="padding: 2px;">@Html.CheckBox("ck-CARTYPE-" + @item.carrierID, Model.FirstOrDefault(a => a.carrierID == item.carrierID) != null, new { @class = "cbcheck" }) @item.carrierName </label>
                        </div>
                        <div class="span3">
                            @{ 
                        var krmspGetPostCodeAlertsResult = Model.FirstOrDefault(a => a.carrierID == item.carrierID);
                        var lsCarrierReason = new SelectList(carrierReasons, "Id", "Name", krmspGetPostCodeAlertsResult != null ? krmspGetPostCodeAlertsResult.CarrierReason : "");
                                @Html.DropDownList("dr-CARTYPE-" + @item.carrierID, lsCarrierReason, new { @class = "input-block-level" })
                            }
                        </div>
                        if (d == 2)
                        {
                        @Html.Raw(@close)
                            d = 0;
                        }
                    }
                    @if (!isClose)
                    {
                        @Html.Raw(@close) 
                    }
                </div>
                <div class="span3" style="width: 19.076923076923077%">
                    <div class="row-fluid">
                        <div class="span12"><span style="font-size: 14px; font-weight: bold">Delivery types</span> </div>
                    </div>
                    @foreach (var item in delTypes)
                    {
                        <div class="row-fluid">
                            <div class="span12">
                                <label style="padding: 2px;">@Html.CheckBox("ck-DELTYPE-" + @item.deliveryTypeID, Model.FirstOrDefault(a => a.deliveryTypeID == item.deliveryTypeID) != null, new { @class = "cbcheck" }) @item.deliveryTypeName </label>
                            </div>
                        </div>
                    }
                    <div class="row-fluid">
                        <div class="span12">
                            <input type="button" name="" value="Save" onclick="SubmitPostCode('#fr-postcodeitem-@Model.First().outcode')" class="btn btn-success" />
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>
